\chapter{Simulation and Coordination Algorithms}
\label{app:algorithms}

\section{Hierarchical Agent Loop}

\begin{algorithm}[H]
    \caption{Sense--Plan--Act with Verification}
    \begin{algorithmic}[1]
        \STATE \textbf{Input:} Agent $i$, level $\ell$, policy parameters $\Theta$, governance shield $S_i$
        \STATE $o \leftarrow$ observe environment, neighbors, and upstream directives
        \STATE $c \leftarrow$ construct context vector from $o$
        \STATE $a \leftarrow$ select option/tool via LinUCB (Chapter~\ref{chap:multiagent})
        \IF{$S_i(c, a)$ violation}
            \STATE escalate to supervisor, draw safe fallback action
        \ENDIF
        \STATE execute action, log outcome and random seeds
        \STATE update local model and share summaries upward
    \end{algorithmic}
\end{algorithm}

\section{Instrumentation Pipeline}

\begin{algorithm}[H]
    \caption{Online Metric Logging}
    \begin{algorithmic}[1]
        \STATE Initialize buffers for state snapshots, entropy estimates, queue statistics
        \FOR{each time step $t$}
            \STATE record microstates $x_i(t)$ and aggregates $\Phi_\ell(t)$
            \STATE update streaming entropy estimators via plug-in or Kozachenko--Leonenko methods
            \STATE accumulate transition counts for transfer entropy calculation
            \STATE compute queue metrics and safety indicators
            \STATE flush batches to persistent storage with metadata (configuration ID, git hash)
        \ENDFOR
    \end{algorithmic}
\end{algorithm}

\section{Experiment Orchestration}

\begin{algorithm}[H]
    \caption{Parameter Sweep Scheduler}
    \begin{algorithmic}[1]
        \STATE Load configuration schema and parameter grid
        \FOR{each parameter tuple $\theta$}
            \FOR{replicate $r = 1$ to $R$}
                \STATE set random seed $\text{seed} = f(\theta, r)$
                \STATE launch simulation job (containerized)
                \STATE collect outputs, validate checksums, register run metadata
            \ENDFOR
        \ENDFOR
        \STATE trigger post-processing workflows and summary report generation
    \end{algorithmic}
\end{algorithm}

\section{Governance Escalation}

\begin{algorithm}[H]
    \caption{Escalation Workflow}
    \begin{algorithmic}[1]
        \STATE monitor for violations of safety predicates $\varphi$
        \IF{violation detected}
            \STATE log event with context snapshot
            \STATE notify designated supervisor; pause affected subsystems if required
            \STATE supervisor evaluates mitigation options; update rule parameters
            \STATE record decision in governance log and update risk register
        \ENDIF
    \end{algorithmic}
\end{algorithm}

